package com.shwread.android.qysw.step.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shwread.android.qysw.activity.healthy.FriendPKDetailActivity;
import com.shwread.android.qysw.step.dto.HMMatch;
import com.shwread.android.qysw.step.dto.LqswHealthMatch;
import com.shwread.android.qysw.step.dto.Player;
import com.shwread.android.qysw.step.model.UserHealthyModel;
import com.shwread.android.qysw.step.view.RectImageView;
import com.shwread.android.qysw.tools.Screen;
import com.shwread.android.qysw.tools.TimeHelper;
import com.shwread.android.qysw10000227.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Fantasy on 16/4/2.
 */
public class FriendPKAdapter extends LoadMoreListViewAdapter {
    Context mContext;
    boolean mIsHistory;

    public FriendPKAdapter(Context context, boolean isHistory) {
        this.mContext = context;
        this.mIsHistory = isHistory;
    }

    @Override
    public RecyclerView.ViewHolder subOnCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.h_pk_friend_item, null);
        rootView.setLayoutParams(new RecyclerView.LayoutParams(Screen.getScreenWidth(), ViewGroup.LayoutParams.WRAP_CONTENT));
        RecyclerView.ViewHolder itemViewHolder = new FriendPKViewHolder(rootView);
        return itemViewHolder;
    }

    @Override
    public void subOnBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FriendPKViewHolder friendPKViewHolder = (FriendPKViewHolder) holder;
        final HMMatch hmMatch = (HMMatch) getTotal_data_list().get(position);
        LqswHealthMatch lqswHealthMatch = hmMatch.getLqswHealthMatch();
        int ownerId = lqswHealthMatch.getOwnerId();

        if (TimeHelper.isToday(lqswHealthMatch.getStartTime())) {
            friendPKViewHolder.datePkText.setText("今日PK");
        } else {
            friendPKViewHolder.datePkText.setText("明日PK");
        }
        int state = hmMatch.getLqswHealthMatch().getState();
        if (state == 1) {
            friendPKViewHolder.dateTimeText.setText("距离比赛开始还有" + TimeHelper.getMyFriendCountDownTime(lqswHealthMatch.getStartTime()));
        } else {
            friendPKViewHolder.dateTimeText.setText("距离结束还有" + TimeHelper.getMyFriendCountDownTime(lqswHealthMatch.getEndTime()));
        }

        List<Player> players = hmMatch.getPlayers();
        for (int i = 0; i < players.size(); i++) {
            if (UserHealthyModel.getInstance().getMyHmUserInfo().getUserId() == players.get(i).getUserId()) {
                friendPKViewHolder.leftNameText.setText(players.get(i).getNickName());
                if (!TextUtils.isEmpty(players.get(i).getUserHead())) {
                    Glide.with(mContext).load(players.get(i).getUserHead()).into(friendPKViewHolder.leftHeadIconImg);
                }
            } else {
                friendPKViewHolder.rightNameText.setText(players.get(i).getNickName());
                if (!TextUtils.isEmpty(players.get(i).getUserHead())) {
                    Glide.with(mContext).load(players.get(i).getUserHead()).into(friendPKViewHolder.rightHeadIconImg);
                }
            }
        }

        if (mIsHistory) {
            friendPKViewHolder.resultImg.setVisibility(View.VISIBLE);
            friendPKViewHolder.centerText.setVisibility(View.INVISIBLE);
            friendPKViewHolder.topLayout.setVisibility(View.INVISIBLE);
            friendPKViewHolder.bottomLayout.setVisibility(View.GONE);
            friendPKViewHolder.stepLayout.setVisibility(View.VISIBLE);
            int createStep = 0, otherStep = 0, myStep = 0, otherss = 0;
            for (int i = 0; i < players.size(); i++) {
                if (players.get(i).getUserId() == UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()) {
                    createStep = players.get(i).getStep();
                } else {
                    otherStep = players.get(i).getStep();
                }
            }

            for (int i = 0; i < players.size(); i++) {
                if (players.get(i).getUserId() == UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()) {
                    myStep = players.get(i).getStep();
                } else {
                    otherss = players.get(i).getStep();
                }
            }
            if (myStep > otherss) {
                friendPKViewHolder.resultImg.setImageResource(R.drawable.h_win_icon);
            } else if (myStep == otherss) {
                friendPKViewHolder.resultImg.setImageResource(R.drawable.equla_yy);
            } else {
                friendPKViewHolder.resultImg.setImageResource(R.drawable.lose_yy);
            }
            friendPKViewHolder.leftStep.setText(createStep + "");
            friendPKViewHolder.rightStep.setText(otherStep + "");
            friendPKViewHolder.pkItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, FriendPKDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("hmMatch", hmMatch);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
        } else {
            int judge = 0;
            int myState = 0;
            for (int i = 0; i < players.size(); i++) {
                if (players.get(i).getIsAccept() == 0 && players.get(i).getUserId() == UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()) {
                    myState = 1;
                }
                if (players.get(i).getIsAccept() == 0 && players.get(i).getUserId() != UserHealthyModel.getInstance().getMyHmUserInfo().getUserId()) {
                    friendPKViewHolder.centerText.setText("等待对方接受PK");
                    judge++;
                }
            }
            //int state = hmMatch.getLqswHealthMatch().getState();//(1),未开始，(2)开始，(3)结束，(4)失效
            if (judge == 0) {//好友已接受
                if (state == 1) {
                    if (myState == 1) {
                        friendPKViewHolder.centerText.setText("点击接受比赛");
                    } else {
                        friendPKViewHolder.centerText.setText("明日开启");
                    }
                } else {
                    friendPKViewHolder.centerText.setText("比赛进行中");
                }

            }

            friendPKViewHolder.pkItemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, FriendPKDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("hmMatch", hmMatch);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });
        }

    }

    static class FriendPKViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.result_img)
        ImageView resultImg;
        @InjectView(R.id.step_layout)
        LinearLayout stepLayout;
        @InjectView(R.id.left_step)
        TextView leftStep;
        @InjectView(R.id.right_step)
        TextView rightStep;
        @InjectView(R.id.top_layout)
        LinearLayout topLayout;
        @InjectView(R.id.bottom_layout)
        LinearLayout bottomLayout;
        @InjectView(R.id.date_pk_text)
        TextView datePkText;
        @InjectView(R.id.date_time_text)
        TextView dateTimeText;
        @InjectView(R.id.left_head_icon_img)
        RectImageView leftHeadIconImg;
        @InjectView(R.id.center_text)
        TextView centerText;
        @InjectView(R.id.right_head_icon_img)
        RectImageView rightHeadIconImg;
        @InjectView(R.id.pk_item_layout)
        FrameLayout pkItemLayout;
        @InjectView(R.id.left_name_text)
        TextView leftNameText;
        @InjectView(R.id.right_name_text)
        TextView rightNameText;

        FriendPKViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
